<?php


namespace app\common\business;


use think\Exception;

class SpecsValue
{
    public $specsValueModel = null;

    public function __construct() {
        //初始化模型对象
        $this->specsValueModel = new \app\common\model\mysql\SpecsValue();
    }

    public function add($data) {
        if (empty($data)) {
            return false;
        }
        $res = (new Specs())->getSpecsById($data['specs_id']);
        if (!$res) {
            return 0;
        }
        try {
            $res = $this->specsValueModel->add($data);
        } catch (\Exception $e) {
            return 0;
        }
        return $res;

    }

    /**
     * 根据ID获取规则下的属性
     * @param $specsId
     * @return array|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBySpecsId($specsId) {
        //验证specs是否存在
        $res = (new Specs())->getSpecsById($specsId);
        if (!$res) {
            return false;
        }
        try {
            $res = $this->specsValueModel->getBySpecsId($specsId);
        }catch (\Exception $e) {
            return false;
        }
        return $res->toArray();
    }

    public function dealGoodsSkus($gids, $flagValue) {
        $specsValueKeys = array_keys($gids);
        //$specsValues = [];
        foreach($specsValueKeys as $specsValueKey) {
            $specsValueKey = explode(",", $specsValueKey);
            foreach($specsValueKey as $k => $v) {
                $new[$k][] = $v;
                $specsValueIds[] = $v;
            }
        }

        $specsValueIds = array_unique($specsValueIds);
        $specsValues = $this->getNormalInIds($specsValueIds);

        $flagValue = explode(",", $flagValue);
        $result = [];

        foreach($new as $key => $newValue) {
            $newValue = array_unique($newValue);
            $list = [];
            foreach($newValue as $vv) {
                $list[] = [
                    "id" => $vv,
                    "name" => $specsValues[$vv]['name'],
                    "flag" => in_array($vv, $flagValue) ? 1 : 0,
                ];
            }
            $result[$key]  =  [
                "name" => $specsValues[$newValue[0]]['specs_name'],
                "list" => $list,
            ];

        }

        return $result;

    }

    public function getNormalInIds($ids) {
        if(!$ids) {
            return [];
        }
        try {
            $result = $this->specsValueModel->getNormalInIds($ids);
        }catch (\Exception $e) {
            return [];
        }
        $result = $result->toArray();
        if(!$result) {
            return [];
        }
        $specsNames = (new \app\common\model\mysql\Specs())->select()->toArray();
        $specsNamesArrs = array_column($specsNames, "name", "id");
        $res = [];
        foreach ($result as $resultValue) {
            $res[$resultValue['id']] = [
                'name' => $resultValue['name'],
                'specs_name' => $specsNamesArrs[$resultValue['specs_id']] ?? "",
            ];
        }
        return $res;

    }

    public function dealSpecsValue($skuIdSpecsValueIds) {
        $ids = array_values($skuIdSpecsValueIds);
        $ids = implode(",", $ids);
        $ids = array_unique(explode(",", $ids));


        $result = $this->getNormalInIds($ids);
        if(!$result) {
            return [];
        }

        $res = [];
        foreach ($skuIdSpecsValueIds as $skuId => $specs) {
            // 1, 7
            $specs = explode(",", $specs);
            //$specs  [1,7]
            // 处理sku默认文案
            $skuStr = [];
            foreach ($specs as $spec) {
                $skuStr[] = $result[$spec]['specs_name'].":".$result[$spec]['name'];
            }
            $res[$skuId] = implode("  ", $skuStr);
        }
        return $res;
    }
}